package com.sxrong.carloan.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.sxrong.carloan.R;

import java.util.ArrayList;

/**
 * Created by liuheng on 2015/8/19.
 */
public class PhotoActivity extends Activity {
    TextView album_num;
    private ViewPager mPager;
    private int index;
    private ArrayList<String> imgs = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        mPager = (ViewPager) findViewById(R.id.pager);
        album_num = (TextView) findViewById(R.id.album_num);
        imgs = getIntent().getStringArrayListExtra("imgs");
        index = getIntent().getIntExtra("index", 0);
        album_num.setText((index+1) + " / " + imgs.size());
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        PagerAdapter adapter = null;
        adapter =  new PagerAdapter() {
            @Override
            public int getCount() {
                return imgs.size();
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(PhotoActivity.this);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                Glide.with(PhotoActivity.this).load(imgs.get(position)).fitCenter().into(view);
                container.addView(view);
                return view;
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
         };
        mPager.setAdapter(adapter);
        mPager.setCurrentItem(index);
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                album_num.setText((position +1)+ " / " + imgs.size());
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


}
